/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.subareas.settings;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import programsettings.ProgramSettings;

import softstarter.filehandling.FileCentre;
import softstarter.gui.master.GUIStateController;
import softstarter.gui.master.SoftStarterGUI;
import softstarter.gui.subareas.ButtonFactory;
import softstarter.gui.subareas.settings.adding.AddableItemAddListener;
import softstarter.gui.subareas.settings.adding.AddableRunnableItem;
import softstarter.gui.subareas.settings.itemlist.EditableRunnableItem;
import softstarter.gui.subareas.settings.itemlist.EditableRunnableItemChangeHandler;
import softstarter.gui.subareas.settings.itemlist.EditableRunnableItemList;
import softstarter.gui.subareas.settings.itemlist.ItemRemoveButtonListenerHandler;
import softstarter.launching.RunnableItem;


public class SettingsView implements ItemRemoveButtonListenerHandler, EditableRunnableItemChangeHandler {

    private GUIStateController masterController;
    private SettingsViewController settingsViewController;
    private SettingsViewListener listener;
    private OpenFeedbackFormListener openFeedbacFormListener;
    private OpenHomePageListener openHomePageListener;
    
    private FileCentre fileCentre;
    private JPanel masterContainer;
    
    private EditableRunnableItemList editableRunnableItemList;
    
    private ButtonFactory buttonFactory;
    private JButton saveButton;
    private JButton dontSaveButton;
    private JButton removeOldFilesButton;
    private JButton removeAllButton;
    private JRadioButton hideWindowsFrame;
    private JRadioButton alwaysOnTop;
    
    private JPanel rightControlButtonsPanel;
    private AddableRunnableItem addableRunnableItem;
    
    public SettingsView(GUIStateController masterController) {
        this.masterController = masterController;
        fileCentre = FileCentre.getInstance();
        buttonFactory = new ButtonFactory();
        
        settingsViewController = new SettingsViewController(this, masterController);
        listener = new SettingsViewListener(settingsViewController, this);
        openFeedbacFormListener = new OpenFeedbackFormListener(settingsViewController);
        openHomePageListener = new OpenHomePageListener(settingsViewController);
        
        build();
    }
    
    private void build() {

        masterContainer = new JPanel(new BorderLayout());
        masterContainer.setOpaque(false);
        editableRunnableItemList = new EditableRunnableItemList(this, listener);
        
        buildView();
        buildListWithFilesAndRemoveButtons();
        
    }

    public JPanel getView() {
        return masterContainer;
    }

    private void buildView() {
    	    	
        rightControlButtonsPanel = new JPanel();
        rightControlButtonsPanel.setOpaque(false);
        rightControlButtonsPanel.setLayout(new GridLayout(10, 1));
        rightControlButtonsPanel.setPreferredSize(new Dimension(120, 0));
        
        saveButton = new JButton("Save");//buttonFactory.getNewIconButton("Save", "./resources/SaveButton.png", "Save filenames");
        saveButton.setActionCommand("Save");
        saveButton.addActionListener(listener);
        saveButton.setForeground(Color.ORANGE);
        
        dontSaveButton = new JButton("Reject");
        dontSaveButton.setActionCommand("Reject");
        dontSaveButton.addActionListener(listener);
        
        removeOldFilesButton = new JButton("Remove obsolet");
        removeOldFilesButton.setActionCommand("Remove old files");
        removeOldFilesButton.addActionListener(listener);
        
        removeAllButton = new JButton("Remove all");
        removeAllButton.setActionCommand("Remove all files");
        removeAllButton.addActionListener(listener);

        hideWindowsFrame = new JRadioButton("Hide windows frame (after startup)");
        hideWindowsFrame.setActionCommand("Show decorations");
        hideWindowsFrame.addActionListener(listener);
        hideWindowsFrame.setSelected(ProgramSettings.getInstance().getShowWindowDecorations());
        
        alwaysOnTop = new JRadioButton("Always on top");
        alwaysOnTop.setActionCommand("Always on top");
        alwaysOnTop.addActionListener(listener);
        alwaysOnTop.setSelected(ProgramSettings.getInstance().isAlwaysOnTop());
        
        JButton indexAFolder = new JButton("Index a folder...");
        indexAFolder.setActionCommand("Index a folder");
        indexAFolder.addActionListener(listener);
        
        JPanel southPanel = new JPanel();
        southPanel.setOpaque(false);
        southPanel.setLayout(new BorderLayout());
        
        JPanel hintPanel = new JPanel();
        hintPanel.setOpaque(false);
        hintPanel.setLayout(new BoxLayout(hintPanel, BoxLayout.Y_AXIS));
        hintPanel.add(new JLabel("<html><b>Tips:"));
        hintPanel.add(new JLabel("Use the hotkey \"win + s\""));
        hintPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 0, 0, Color.LIGHT_GRAY), BorderFactory.createEmptyBorder(0, 5, 0, 5)));
        hintPanel.setToolTipText("Sorry, can't change this yet.");
        
        JPanel programSettingsPane = new JPanel();
        programSettingsPane.setOpaque(false);
        programSettingsPane.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
        programSettingsPane.setLayout(new BoxLayout(programSettingsPane, BoxLayout.Y_AXIS));
        programSettingsPane.add(new JLabel("Settings"));
        programSettingsPane.add(hideWindowsFrame);
        programSettingsPane.add(alwaysOnTop);
        
        JLabel versionLabel = new JLabel("<html><b>" + SoftStarterGUI.FIRESOFT_VERSION);
        versionLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
        versionLabel.setPreferredSize(new Dimension(120, 0));
        
        JLabel giveFeedbackLabel = new JLabel("<html>Give feedback");
        giveFeedbackLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.ORANGE), BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        giveFeedbackLabel.setToolTipText("Click to go to feedback form");
        giveFeedbackLabel.addMouseListener(openFeedbacFormListener);
        giveFeedbackLabel.setPreferredSize(new Dimension(120, 0));
        
        JLabel byLabel = new JLabel("by Arttu Viljakainen");
        byLabel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        byLabel.setPreferredSize(new Dimension(120, 0));
        byLabel.addMouseListener(openHomePageListener);
        byLabel.setToolTipText("Click to open home page/blog");
        
        JPanel infoAndFeedbackLabelContainer = new JPanel();
        infoAndFeedbackLabelContainer.setOpaque(false);
        infoAndFeedbackLabelContainer.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
        infoAndFeedbackLabelContainer.setLayout(new BoxLayout(infoAndFeedbackLabelContainer, BoxLayout.Y_AXIS));
        infoAndFeedbackLabelContainer.add(versionLabel);
        infoAndFeedbackLabelContainer.add(byLabel);
        infoAndFeedbackLabelContainer.add(giveFeedbackLabel);
        
        southPanel.add(programSettingsPane, BorderLayout.WEST);
        southPanel.add(hintPanel, BorderLayout.CENTER);
        southPanel.add(infoAndFeedbackLabelContainer, BorderLayout.EAST);
        
        addableRunnableItem = new AddableRunnableItem(new RunnableItem("Placeholder", "http://"));
        addableRunnableItem.addAddButtonListener(new AddableItemAddListener(settingsViewController));
        
        JPanel centrePanel = new JPanel();
        centrePanel.setOpaque(false);
        centrePanel.setLayout(new BorderLayout());
        centrePanel.add(editableRunnableItemList.getView(), BorderLayout.CENTER);
        centrePanel.add(addableRunnableItem.getView(), BorderLayout.SOUTH);
        
        
        masterContainer.add(rightControlButtonsPanel, BorderLayout.EAST);
        masterContainer.add(southPanel, BorderLayout.SOUTH);
    	masterContainer.add(centrePanel, BorderLayout.CENTER);

        showBasicButtons();

    }
    
    public void buildListWithFilesAndRemoveButtons() {
        
    	editableRunnableItemList.updateRunnableItemList(fileCentre.getFiles());
    	showBasicButtons();
        
        masterController.refreshGUI();
    }
    
    public void showSaveButtons() {
    	rightControlButtonsPanel.removeAll();
        rightControlButtonsPanel.add(saveButton);
        rightControlButtonsPanel.add(dontSaveButton);
        masterController.refreshGUI();
    }
    
    public void showBasicButtons() {
    	rightControlButtonsPanel.removeAll();
        rightControlButtonsPanel.add(removeOldFilesButton);
        rightControlButtonsPanel.add(removeAllButton);
        masterController.refreshGUI();
    }

	@Override
	public void removeButton(EditableRunnableItem editableRunnableItem) {
		fileCentre.removeFile(editableRunnableItem.getItem());
		editableRunnableItemList.updateRunnableItemList(fileCentre.getFiles());
		masterController.refreshGUI();
	}

	public EditableRunnableItemList getEditableRunnableItemList() {
		return editableRunnableItemList;
	}

	@Override
	public void itemsChanged() {

		showSaveButtons();
	}

	public AddableRunnableItem getAddableRunnableItem() {
		return addableRunnableItem;
	}

}
